package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Address;
import com.java.second_goods.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址信息
 */
@RestController
@RequestMapping("/api/address")
@Tag(name = "地址管理", description = "地址相关接口")
public class AddressController extends BaseController {

    @Autowired
    private AddressService addressService;

    /**
     * 获取地址列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取地址列表", description = "获取地址列表")
    public AjaxResult list(Address address) {
        List<Address> list = addressService.selectAddressList(address);
        return success(list);
    }

    /**
     * 获取地址详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取地址详细信息", description = "根据地址ID获取地址详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(addressService.selectAddressById(id));
    }

    /**
     * 新增地址
     */
    @PostMapping
    @Operation(summary = "新增地址", description = "新增地址")
    public AjaxResult add(@RequestBody Address address) {
        return toAjax(addressService.insertAddress(address));
    }

    /**
     * 修改地址
     */
    @PutMapping
    @Operation(summary = "修改地址", description = "修改地址")
    public AjaxResult edit(@RequestBody Address address) {
        return toAjax(addressService.updateAddress(address));
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除地址", description = "删除地址")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(addressService.deleteAddressByIds(ids));
    }

    /**
     * 根据用户ID获取地址列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户ID获取地址列表", description = "根据用户ID获取地址列表")
    public AjaxResult getAddressByUserId(@PathVariable("userId") Integer userId) {
        List<Address> addresses = addressService.selectAddressByUserId(userId);
        return success(addresses);
    }

    /**
     * 获取用户默认地址
     */
    @GetMapping("/default/{userId}")
    @Operation(summary = "获取用户默认地址", description = "获取用户默认地址")
    public AjaxResult getDefaultAddress(@PathVariable("userId") Integer userId) {
        Address address = addressService.selectDefaultAddressByUserId(userId);
        return success(address);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default/{addressId}/{userId}")
    @Operation(summary = "设置默认地址", description = "设置默认地址")
    public AjaxResult setDefaultAddress(@PathVariable("addressId") Integer addressId,
                                        @PathVariable("userId") Integer userId) {
        return toAjax(addressService.setDefaultAddress(addressId, userId));
    }
}